package com.lemon.dao;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.SqlSession;

import com.lemon.domain.Employee;
import com.lemon.util.MybatisUtils;

public class EmployeeDao {

	/**
	 * 查找员工
	 * @param id
	 * @return
	 */
	public Employee get(Long id) {
		SqlSession session = MybatisUtils.getSession();
		Employee r;
		
		try {
			r = session.selectOne("com.lemon.mapper.EmployeeMapper.get", id);
		} finally {
			session.close();
		}

		return r;
	}
	
	/**
	 * 根据部门ID查找员工
	 * @param departmentId 部门ID
	 * @return
	 */
	public List<Employee> findEmployeesByDepartmentId(Long departmentId) {
		SqlSession session = MybatisUtils.getSession();
		List<Employee> list;
		
		try {
			list = session.selectList("com.lemon.mapper.EmployeeMapper.findEmployeesByDepartmentId", departmentId);
		} finally {
			session.close();
		}
		
		return list;
	}
	
	/**
	 * 查找角色属性相同的员工
	 * @param roleId
	 * @return
	 */
	public List<Employee> findByRoleId(Long roleId) {
		SqlSession session = MybatisUtils.getSession();
		List<Employee> list;
		
		try {
			list = session.selectList("com.lemon.mapper.EmployeeMapper.findByRoleId", roleId);
		} finally {
			session.close();
		}
		
		return list;
	}
	
	/**
	 * 查找所有员工
	 * @return
	 */
	public List<Employee> list() {
		SqlSession session = MybatisUtils.getSession();
		List<Employee> list;
		
		try {
			list = session.selectList("com.lemon.mapper.EmployeeMapper.list");
		} finally {
			session.close();
		}

		return list;
	}
	
	/**
	 * 登录
	 * @param username
	 * @param password
	 * @return
	 */
	public Employee login(String username, String password) {
		SqlSession session = MybatisUtils.getSession();
		Employee e;
		Map<String, String> param = new HashMap<>();
		param.put("username", username);
		param.put("password", password);
		
		try {
			e = session.selectOne("com.lemon.mapper.EmployeeMapper.login", param);
		} finally {
			session.close();
		}
		
		return e;
	}
	
	/**
	 * 插入
	 * @param e
	 * @return
	 */
	public Long save(Employee e) {
		SqlSession session = MybatisUtils.getSession();
		
		try {
			session.insert("com.lemon.mapper.EmployeeMapper.save", e);
			session.commit();
		} finally {
			session.close();
		}
		
		return e.getId();
	}
	
	/**
	 * 删除
	 * @param id
	 * @return
	 */
	public int delete(Long id) {
		SqlSession session = MybatisUtils.getSession();
		int rows;
		
		try {
			rows = session.delete("com.lemon.mapper.EmployeeMapper.delete", id);
			session.commit();
		} finally {
			session.close();
		}
		
		return rows;
	}
	
	/**
	 * 修改
	 * @param e
	 * @return
	 */
	public int update(Employee e) {
		SqlSession session = MybatisUtils.getSession();
		int rows;
		
		try {
			rows = session.update("com.lemon.mapper.EmployeeMapper.update", e);
			if (rows == 1) {// 如果没有记录被修改，那么该事物就不用提交了
				session.commit();
			} 
		} finally {
			session.close();
		}
		
		return rows;
	}
	
	/**
	 * 为员工分配角色
	 * @param e
	 * @return
	 */
	public int addRole(Employee e) {
		SqlSession session = MybatisUtils.getSession();
		int rows;
		
		try {
			rows = session.update("com.lemon.mapper.EmployeeMapper.addRole", e);
			if (rows == 1) {// 如果没有记录被修改，那么该事物就不用提交了
				session.commit();
			} 
		} finally {
			session.close();
		}
		
		return rows;
	}
}
